const express = require("express");
const app = express();
const http = require("http").createServer(app);
const io = require("socket.io")(http, {cors: true});

io.on("connection", socket => {  // 客户端链接成功
    console.log('~connection~')
    io.emit("message", {msg: '我是服务器'})
    // setInterval(()=>{
    //     io.emit("message", "我是服务器：" + Date.now());
    // }, 1000)
    socket.on("message", msg => {  // 监听的频道必须和客户端监听的频道相同，等待消息
        io.emit("message", msg);  // 向所有客户端发送信息
    });

    io.on("disconnect", _ => {  // 客户端断开链接
       
    });

});

http.listen(3009, _ => {
    console.log("server running on 3009");
});
